<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2018/6/13
 * Time: 23:51
 */

namespace App\Models\Api;

use App\Models\Api\Base;
use Cache;

class Signtext extends Base
{
    protected $table = 'sign_text';

    public function getAllSignText()
    {
        $sign_text = false;

        if (intval($this->cache_on) >= 1 && Cache::has('app_sign_text'))
        {
            $sign_text = Cache::get('app_sign_text');
        }

        if ($sign_text === false || !$sign_text)
        {
            try
            {
                $sign_text = $this->select('sign_id','planet_id','text')->get();

                $sign_text = json_decode(json_encode($sign_text, true), true);

                if ($this->cache_on >= 1)
                {
                    Cache::set('app_sign_text', $sign_text, 86400 * 3);
                }
            }
            catch (\Exception $e)
            {
                $sign_text = [];
            }

        }
        return $sign_text;
    }

    public function getSignText($map = [])
    {
        $planet_id = $map['planet_id'];
        $sign_id = $map['sign_id'];

        $data = false;

        if (intval($this->cache_on) >= 1 && Cache::has('app_sign_text_' . $planet_id . '-' . $sign_id))
        {
            $data = Cache::get('app_sign_text_' . $planet_id . '-' . $sign_id);
        }

        if ($data === false)
        {
            try{
                $sign_text = $this->select('text')
                    ->where(['sign_id' => $sign_id, 'planet_id' => $planet_id])
                    ->get();

                $sign_text = json_decode(json_encode($sign_text, true), true);

                if ($sign_text || !empty($sign_text))
                {
                    foreach ($sign_text as $v)
                    {
                        $data [] = $v['text'];
                    }
                }
                Cache::set('app_sign_text_' . $planet_id . '-' . $sign_id, $data, 86400 * 3);
            }
            catch (\Exception $e)
            {
                $data = [];
            }
        }
        return $data;
    }

}